package org.aristotle.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.aristotle.common.response.ResponseEntity;
import org.aristotle.common.response.result.Result;
import org.aristotle.common.utils.AristotleBeanUtils;
import org.aristotle.demo.model.entity.Book;
import org.aristotle.demo.service.IBookService;
import org.aristotle.demo.model.vo.BookVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("book")
@Api(value="图书controller",tags={"图书操作接口"})
public class BookController {

    @Autowired
    private IBookService bookService;

    /*分页查询*/
    @GetMapping("page")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "query", value = "图书id", dataType = "Long", required = false),
            @ApiImplicitParam(name = "title", paramType = "query", value = "图书名", dataType = "String", required = false),
            @ApiImplicitParam(name = "categoryId", paramType = "query", value = "图书名", dataType = "Long", required = false),
            @ApiImplicitParam(name = "current", paramType = "query", value = "页码", dataType = "Integer", required = false,defaultValue = "1"),
            @ApiImplicitParam(name = "size", paramType = "query", value = "页距", dataType = "Integer", required = false,defaultValue = "10")
    })
    public Result<Page<BookVO>> queryPageList(
            @RequestParam(name = "id",required = false) Long id,
            @RequestParam(name = "title",required = false) String title,
            @RequestParam(name = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "10") Integer size
    ) {
        Page<BookVO> result = bookService.queryPageList(id,title,categoryId,current,size);
        return ResponseEntity.ok(result);
    }

    @GetMapping
    @ApiOperation("根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "query", value = "图书id", dataType = "Long", required = true)
    })
    public Result<?> queryById(@RequestParam(name = "id") Long id) {
        if (id == null) {
            return ResponseEntity.badRequest();
        }
        BookVO result = bookService.getById(id);
        if (result == null) {
            return ResponseEntity.notFound();
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("list")
    @ApiOperation("获取列表")
    public Result<?> queryList() {
        return ResponseEntity.ok(AristotleBeanUtils.copyListProperties(bookService.list(),BookVO::new));
    }

    @PostMapping
    @ApiOperation("添加")
    public Result<?> add(@RequestBody BookVO bookVO) {
        Book book = AristotleBeanUtils.transitionBean(bookVO, Book::new);
        book.setId(null);
        bookService.save(book);
        return ResponseEntity.addOk(bookVO);
    }

    @PutMapping
    @ApiOperation("修改")
    public Result<?> edit(@RequestBody BookVO bookVO) {
        if (bookVO.getId() == null) {
            return ResponseEntity.badRequest();
        }
        Book book = AristotleBeanUtils.transitionBean(bookVO, Book::new);
        bookService.updateById(book);
        return ResponseEntity.updateOk(bookVO);
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "path", value = "图书id", dataType = "Long", required = true)
    })
    public Result<?> delete(@PathVariable Long id) {
        if (id == null) {
            return ResponseEntity.badRequest();
        }
        bookService.removeById(id);
        return ResponseEntity.deleteOk(id);
    }

}
